package com.entfrm.biz.it.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.mapper.PropertyCategoryMapper;
import com.entfrm.biz.it.service.PropertyCategoryService;
import org.springframework.stereotype.Service;

/**
 * @author kyc
 * @date 2020-08-07 14:07:33
 *
 * @description 资产分类Service业务层
 */
@Service
public class PropertyCategoryServiceImpl extends ServiceImpl<PropertyCategoryMapper, PropertyCategory> implements PropertyCategoryService {
            /**
      * 构建树
      *
      * @param list     分类表
      * @param parentId 传入的父节点ID
      * @return String
      */
        @Override
        public List<PropertyCategory> buildTree(List<PropertyCategory> list, int parentId) {
            List<PropertyCategory> propertyCategoryList = new ArrayList<>();
            for (Iterator<PropertyCategory> iterator = list.iterator(); iterator.hasNext(); ) {
                PropertyCategory t = (PropertyCategory) iterator.next();
                if (t.getParentId().intValue() == parentId) {
                    recursion(list, t);
                        propertyCategoryList.add(t);
                }
            }
            return propertyCategoryList;
        }

        /**
       * 递归列表
       *
       * @param list
       * @param t
       */
        private void recursion(List<PropertyCategory> list, PropertyCategory t) {
            // 得到子节点列表
            List<PropertyCategory> childList = getChildList(list, t);
            t.setChildren(childList);
            for (PropertyCategory tChild : childList) {
                if (hasChild(list, tChild)) {
                    // 判断是否有子节点
                    Iterator<PropertyCategory> it = childList.iterator();
                    while (it.hasNext()) {
                        PropertyCategory n = (PropertyCategory) it.next();
                        recursion(list, n);
                    }
                }
            }
        }

        /**
         * 得到子节点列表
         */
        private List<PropertyCategory> getChildList(List<PropertyCategory> list, PropertyCategory t) {
            List<PropertyCategory> tlist = new ArrayList<PropertyCategory>();
            Iterator<PropertyCategory> it = list.iterator();
            while (it.hasNext()) {
                PropertyCategory n = (PropertyCategory) it.next();
                if (n.getParentId().intValue() == t.getId().intValue()) {
                    tlist.add(n);
                }
            }
            return tlist;
        }

        /**
         * 判断是否有子节点
         */
        private boolean hasChild(List<PropertyCategory> list, PropertyCategory t) {
            return getChildList(list, t).size() > 0 ? true : false;
        }

    }
